import {ElLoading, ElMessage, ElMessageBox} from "element-plus";
import axios from "axios";
import VueCookies from "vue-cookies";

axios.defaults.baseURL = 'http://127.0.0.1:8888';
axios.defaults.headers.post = {'Content-Type': 'application/x-www-form-urlencoded'};
axios.defaults.headers.put = {'Content-Type': 'multipart/form-data'};
let timer = null;   // 防抖定时器

// 发送网络请求
export function request(options) {
    return new Promise((t, f) => {
        axios(options).then(res => {
            let {code, msg: message} = res.data;
            switch (code) {
                case 200: t(res.data); break;
                default: msg(message, 'warning'); break;
            }
        }).catch(err => {
            msg('服务异常', 'error');
            console.log(options, err);
        })
    })
}

/**
 * 获取窗口大小
 * @param util 是否携带单位
 */
export function getWindowSize(util = false) {
    let width = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
    let height = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
    return util ? {width: width + 'px', height: height + 'px'} : {width, height};
}

/**
 * 消息通知
 * @param message 通知内容
 * @param type 通知类型 success-成功  warning-警告 info-消息 error-错误
 */
export function msg(message, type = 'success') {
    ElMessage({message, type});
}

/**
 * 消息确认框
 * @param {*} title 标题
 * @param {*} content 内容
 * @param {*} type 类型
 * @param {*} showCancelButton 是否显示取消按钮
 */
export function model(title, content, type, showCancelButton = true) {
    return new Promise((t, f) => {
        ElMessageBox.confirm(content, title, {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type, showCancelButton
        }).then(
            () => {t(true)},
            () => {t(false)}
        );
    })
}

/**
 * 弹出输入框
 * @param title 标题
 * @param content 提示内容
 * @param inputValue 默认文本
 * @param inputPattern 校验规则
 * @param inputErrorMessage 错误提示信息
 * @param inputType 输入框类型
 */
export function input({title, content, inputValue, inputPattern, inputErrorMessage, inputType}) {
    return new Promise(t => {
        ElMessageBox.prompt(content, title, {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            inputValue,inputPattern,inputErrorMessage,inputType
        }).then(({ value }) => {
            t(value);
        });
    })
}

/**
 * 防抖
 * @param fn 回调函数
 * @param delay 延时
 */
export function debounce(fn, delay = 500) {
    return function (...args) {
        clearTimeout(timer);
        timer = setTimeout(() => {
            fn.apply(this, args);
        }, delay);
    }
}

/**
 * 获取 Cookies 数据
 * @param key key
 */
export function getCookies(key) {
    return VueCookies.get(key)
}

/**
 * 设置 Cookies
 * @param key key
 * @param value value
 */
export function setCookies(key, value) {
    VueCookies.set(key, value)
}

/**
 * 移除 Cookies
 * @param key key
 */
export function removeCookies(key) {
    VueCookies.remove(key)
}

// 获取当前字符串日期
export function getNowDate() {
    let date = new Date();
    let year = date.getFullYear();
    let month = (1 + date.getMonth()).toString().padStart(2, '0');
    let day = date.getDate().toString().padStart(2, '0');
    let hours = date.getHours().toString().padStart(2, '0');
    let minutes = date.getMinutes().toString().padStart(2, '0');
    let seconds = date.getSeconds().toString().padStart(2, '0');

    return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
}

// 请求地址
export let baseURL = axios.defaults.baseURL;